package zero.tech.games.user.service;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import zero.tech.core.data.JwtUtil;
import zero.tech.core.data.Resp;
import zero.tech.core.data.SimpleUser;
import zero.tech.core.util.RandomUtil;
import zero.tech.games.common.Code;
import zero.tech.games.common.TUserAccountUtil;
import zero.tech.games.entity.user.TUser;
import zero.tech.games.entity.user.TUserAccount;
import zero.tech.games.user.data.LoginType;
import zero.tech.games.user.data.req.ReqUserLogin;
import zero.tech.games.user.data.res.ResUserLogin;
import zero.tech.games.user.mapper.TUserAccountMapper;
import zero.tech.games.user.mapper.TUserMapper;
import zero.tech.games.user.service.api.IUserService;
import zero.tech.games.user.util.PackageUtil;

@Service
public class UserServiceImpl implements IUserService{
	@Autowired
	private TUserMapper userMapper;
	@Autowired
	private TUserAccountMapper userAccountMapper;
	@Autowired
	private TUserAccountUtil userAccountUtil;
	@Override
	@Transactional
	public Resp userLogin(ReqUserLogin reqUserLogin) {
		Integer login_type=reqUserLogin.getLogin_type();
		boolean newplayer=false;
		TUser user=null;
		TUserAccount userAccount=null;
		if(login_type==LoginType.PHONE_LOGIN) {
			String phone=reqUserLogin.getPhone();
			if(StringUtils.isBlank(phone)) {
				return Resp.build(Code.PARAM_ERROR, "手机号为null");
			}
			String pwd=reqUserLogin.getPwd();
			user=userMapper.selectUserByPhone(phone);
			if(user==null) {
				user=newUserByPhone(phone,pwd);
				userMapper.insert(user);
				user.setNick("guest"+user.getUid());
				userMapper.update(user);
				newplayer=true;
			}else {
				if(!pwd.equals(user.getPwd())) {
					return Resp.build(Code.PASSWORD_ERROR, "密码错误");
				}
			}
		}else {
			throw new RuntimeException("找不到登陆类型");
		}
		if(newplayer) {
			//初始化新玩家账户
			userAccount=initNewUserAccount(user);
			userAccountUtil.insertOrUpdate(userAccount);
		}else {
			userAccount=userAccountUtil.getUserAccount(user.getUid());
		}
		//登陆成功
		ResUserLogin resUserLogin=loginSuccess(user,userAccount);
		return Resp.build(Code.OK, "登陆成功",resUserLogin);
	}

	private TUserAccount initNewUserAccount(TUser user) {
		int uid=user.getUid();
		long diamond=0;
		TUserAccount userAccount=new TUserAccount(uid, diamond);
		userAccountMapper.insert(userAccount);
		return userAccount;
	}

	private TUser newUserByPhone(String phone, String pwd) {
		Date now=new Date();
		TUser user=new TUser();
		user.setPhone(phone);
		user.setPwd(pwd);
		user.setAi(0);
		user.setSex(0);
		user.setRegist_time(now);
		user.setNick("guest");
		user.setIdno(RandomUtil.randInt(10000)+"");
		user.setHead_img("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202008%2F08%2F20200808135140_THdwT.thumb.400_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1637825898&t=cc38c4ed247d31b00240e84da862588e");
		return user;
	}

	private ResUserLogin loginSuccess(TUser user, TUserAccount userAccount) {
		ResUserLogin resUserLogin=new ResUserLogin();
		SimpleUser simpleUser=PackageUtil.packageSimpleUser(user);
		String token=JwtUtil.createJWT(24*60*60*1000,simpleUser );
		resUserLogin.setToken(token);
		resUserLogin.setSimpleUser(simpleUser);
		return resUserLogin;
	}
}
